//
//  CalendarView.m
//  DigitalClock
//
//  Created by Kishikawa Katsumi on 10/04/04.
//  Copyright 2010 Kishikawa Katsumi. All rights reserved.
//

#import "CalendarView.h"

@implementation CalendarView

@synthesize baseDate;
@synthesize weekStartsMonday;
@synthesize highlightHolidays;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        self.backgroundColor = [UIColor clearColor];
        calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        fullDateFormatter = [[NSDateFormatter alloc] init];
        [fullDateFormatter setDateFormat:@"yyyyMMdd"];
    }
    return self;
}

- (void)dealloc {
    [calendar release];
    [fullDateFormatter release];
    [baseDate release];
    [super dealloc];
}

- (void)drawRect:(CGRect)rect {
    NSDate *now = [NSDate date];
    NSDateComponents *components = [calendar components:
                                    (NSDayCalendarUnit | 
                                     NSWeekdayCalendarUnit | 
                                     NSMonthCalendarUnit | 
                                     NSYearCalendarUnit) fromDate:baseDate];
	[components setDay:1];
	NSInteger month = [components month];
	
	NSDate *date = [calendar dateFromComponents:components];
	
	NSDateComponents *aDay = [[[NSDateComponents alloc] init] autorelease];
	[aDay setDay:1];
	
    BOOL shouldDrawWeekdayLabel = YES;
	NSUInteger weekOfMonth = 0;
	do {
		components = [calendar components:(NSDayCalendarUnit | NSWeekdayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:date];
        
		NSString *day = [NSString stringWithFormat:@"%d", [components day]];
		NSInteger weekday = [components weekday];
        if (weekStartsMonday) {
            weekday = weekday == 1 ? 7 : weekday - 1;
        }
		
		NSString *fullDate = [NSString stringWithFormat:@"%04d%02d%02d", [components year], [components month], [components day]];
        
        if (shouldDrawWeekdayLabel) {
            shouldDrawWeekdayLabel = NO;
            for (int i = 0; i < 7; i++) {
                int dayIndex = i + 1;
                if (weekStartsMonday) {
                    dayIndex = dayIndex == 7 ? 1 : dayIndex + 1;
                }

                UIImage *weekImage = [UIImage imageNamed:[NSString stringWithFormat:@"week_%ds.png", dayIndex]];
                [weekImage drawAtPoint:CGPointMake(28.0f + 39.0f * i, 20.0f)];
            }
        }
        
        CGPoint point = CGPointMake(27.0f + 39.0f * (weekday - 1), 
                                 49.0f + 35.0f * weekOfMonth);
        
        BOOL isToday = [fullDate isEqualToString:[fullDateFormatter stringFromDate:now]];
        
        UIImage *dayImage;
        if (isToday) {
			dayImage = [UIImage imageNamed:[NSString stringWithFormat:@"today_%@.png", day]];
		} else if ((!weekStartsMonday && weekday == 1) || (weekStartsMonday && weekday == 7)) {
			dayImage = [UIImage imageNamed:[NSString stringWithFormat:@"cal_holiday_%@.png", day]];
		} else {
			dayImage = [UIImage imageNamed:[NSString stringWithFormat:@"cal_%@.png", day]];
		}

        [dayImage drawAtPoint:point];
        
		date = [calendar dateByAddingComponents:aDay toDate:date options:0];
		components = [calendar components:(NSDayCalendarUnit | 
                                           NSWeekdayCalendarUnit | 
                                           NSMonthCalendarUnit |
                                           NSYearCalendarUnit) fromDate:date];
		if (weekday == 7) {
			weekOfMonth++;
		}
	} while (month == [components month]);
}

- (void)update {
    [self setNeedsDisplay];
}

@end
